package com.zhimeng.personal.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zhimeng.personal.data.DataCenter;
import com.zhimeng.personal.models.User;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunzhimeng on 16/4/5.
 */
public class JSONUtils {
    public static List<JSONObject> getObjectsFromJArray(JSONArray arrayList) {
        List<JSONObject> olistArrays = new ArrayList<JSONObject>();
        for (int i = 0;i < arrayList.length();i ++){
            olistArrays.add((JSONObject)arrayList.opt(i));
        }
        return olistArrays;
    }

    private static Gson gson;

    public static boolean saveUserToJson(User user, Context context) {
        gson = new Gson();
        String homeStr = gson.toJson(user);
        return saveDataToFile(homeStr, SDCardUtils.getLogCacheDir(context) + File.separator + "jsondata" + File.separator + "current_user.txt");
    }

    private static boolean saveDataToFile(String dataStr, String dir) {
        boolean flag = false;
        File file = new File(dir);
        if (!file.getParentFile().exists()) {//判断父文件是否存在，如果不存在则创建
            file.getParentFile().mkdirs();
        } else {

        }
        PrintStream out = null; //打印流
        try {
            out = new PrintStream(new FileOutputStream(file)); //实例化打印流对象
            out.print(dataStr.toString()); //输出数据
            flag = true;
        } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
            e.printStackTrace();

        } finally {
            if (out != null) { //如果打印流不为空，则关闭打印流
                out.close();
            }
        }
        return flag;
    }

    public static User getUserFromJson(Context context) {
        User user = DataCenter.visiter();
        String path = SDCardUtils.getLogCacheDir(context);
        Reader reader = null;
        BufferedReader br = null;
        try {
            reader = new FileReader(path + File.separator + "jsondata" + File.separator + "current_user.txt");
            br = new BufferedReader(reader);
            StringBuffer sb = new StringBuffer();
            String data = null;
            while ((data = br.readLine()) != null) {
                sb.append(data);
            }
            String productsStr = sb.toString();
            gson = new GsonBuilder().create();
            user = gson.fromJson(productsStr, User.class);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (br != null) {

                    br.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return user;
    }
}
